# client.py
import socket

# 服务器地址和端口
SERVER_HOST = 'localhost'
SERVER_PORT = 32345


def start_client(message):
    try:
        # 1. 创建TCP套接字
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        # 2. 连接服务器
        client_socket.connect((SERVER_HOST, SERVER_PORT))
        print(f'已连接到服务器 {SERVER_HOST}:{SERVER_PORT}')

        # 3. 发送数据给服务器
        # message = 'WRITE|PUB_VARIBLE|頂頂頂頂'
        client_socket.sendall(message.encode())  # encode：把字符串数据转换成字节数据

        # 4. 接收服务器的响应
        data = client_socket.recv(1024)
        print(f'收到服务器消息: {data.decode()}')  # decode：把字节数据转换成字符串数据

    except Exception as e:
        print(f'客户端出错: {e}')
    finally:
        # 5. 关闭客户端套接字
        client_socket.close()


if __name__ == "__main__":
    # start_client('WRITE|PUB_VARIBLE_UI_STOP|3333')
    # start_client('READ|PUB_VARIBLE_UI_STOP|')
    # start_client('WRITE|PUB_VARIBLE_UI_STOP|地對地導彈')
    # start_client('READ|PUB_VARIBLE_UI_STOP|')
    for i in range(1,909):
        start_client('WRITE|PUB_VARIBLE_UI_STOP|' + str(i))
        start_client('READ|PUB_VARIBLE_UI_STOP|')

